﻿//******************************************************************************************************
//  SignalProxyBase.cs - Gbtc
//
//  Copyright © 2013, Grid Protection Alliance.  All Rights Reserved.
//
//  Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
//  the NOTICE file distributed with this work for additional information regarding copyright ownership.
//  The GPA licenses this file to you under the Eclipse Public License -v 1.0 (the "License"); you may
//  not use this file except in compliance with the License. You may obtain a copy of the License at:
//
//      http://www.opensource.org/licenses/eclipse-1.0.php
//
//  Unless agreed to in writing, the subject software distributed under the License is distributed on an
//  "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
//  License for the specific language governing permissions and limitations.
//
//  Code Modification History:
//  ----------------------------------------------------------------------------------------------------
//  04/23/2013 - Stephen C. Wills
//       Generated original version of source code.
//
//******************************************************************************************************

using System;
using System.Collections.Generic;

namespace SIEGate.Core.Notifications
{
    public class NotificationReceiver : INotificationReceiver
    {
        #region [ Members ]

        // Fields
        protected NotificationDispatcher m_dispatcher;
        private INotificationTarget m_notificationTarget;
        private Action<IEnumerable<object>>[] m_broadcastSignalHandlers;
        private uint m_targetID;
        private Dictionary<string, Action<IEnumerable<object>>> m_targetedHandlers;
        #endregion

        #region [ Constructors ]

        internal NotificationReceiver(NotificationDispatcher dispatcher, uint targetID)
        {
            m_dispatcher = dispatcher;
            m_targetID = targetID;
            m_dispatcher.Broadcasts += m_dispatcher_Broadcasts;
            m_broadcastSignalHandlers = new Action<IEnumerable<object>>[Enum.GetValues(typeof(BroadcastNotification)).Length];
            m_targetedHandlers = new Dictionary<string, Action<IEnumerable<object>>>();
        }

        void m_dispatcher_Broadcasts(object sender, BroadcastNotificationArgs e)
        {
            try
            {
                Action<IEnumerable<object>> func = m_broadcastSignalHandlers[(int)e.Message];
                func(e.Arguments);
            }
            catch
            {
            }
        }

        #endregion

        #region [ Properties ]

        #endregion

        #region [ Methods ]

        public virtual void RegisterBroadcastHandler(BroadcastNotification message, Action<IEnumerable<object>> handler)
        {
            m_broadcastSignalHandlers[(int)message] = handler;
        }
        internal void MessageReceived(TargetedNotififcationArgs e)
        {
            Action<IEnumerable<object>> handler;
            if (m_targetedHandlers.TryGetValue(e.Message, out handler))
            {
                try
                {
                    handler(e.Arguments);
                }
                catch
                {
                }
            }
        }

        public void RegisterTargetedHandler(string message, Action<IEnumerable<object>> handler)
        {
            m_targetedHandlers[message] = handler;
        }

        public void SendNotification(uint targetID, string message, params object[] arguments)
        {
            m_dispatcher.SendSignal(targetID, message, arguments);
            LogDispatcher.W("NotificationReciever", "notifications", "Sending target:{0} a notification: {1}", targetID, message);
        }

        #endregion
    }
}
